package com.github.shiqiyue.kibana.translator.zhcn;

import com.github.shiqiyue.kibana.translator.zhcn.util.PathUtils;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

/***
 * 翻译器
 * @author wenyao.wu
 * @date 2018/7/27
 */
public class Translator {

    public static void translate(File file, String source, String target) throws IOException {
        if (!file.exists()) {
            TranslatorContext.notFindFileSet.add(file.getAbsolutePath());
            return;
        }

        if (file.isDirectory()) {
            File[] sonFiles = file.listFiles();
            if (sonFiles == null || sonFiles.length == 0) {
                return;
            }
            for (File sonFile : sonFiles) {
                translate(sonFile, source, target);
            }
            return;
        }
        String content = FileUtils.readFileToString(file, StandardCharsets.UTF_8);
        System.out.println("汉化文件:" + file.getAbsolutePath());
        content = content.replace(source, target);
        FileUtils.writeStringToFile(file, content, StandardCharsets.UTF_8);
        TranslatorContext.successSet.add(file.getAbsolutePath());

    }
}
